package com.zhiyizhen.handheld.common;

import static java.lang.Long.max;

import java.util.function.Supplier;

public class ThreadUtil {
    
    private ThreadUtil() {
    }
    
    private static final long MIN_INTERVAL = 100;  // unit: ms

    public static void waitUntil(Supplier<Boolean> stopCondition, long interval) {
        if (stopCondition == null) {
            return;
        }
        interval = max(interval, MIN_INTERVAL);
        while (Boolean.FALSE.equals(stopCondition.get())) {
            try {
                Thread.sleep(interval);
            } catch (InterruptedException e) {
                LogUtil.e(e);
                Thread.currentThread().interrupt();
            }
        }
    }

    public static void waitUntil(Supplier<Boolean> stopCondition) {
        waitUntil(stopCondition, MIN_INTERVAL);
    }
}